package com.cs456.TeamBobcat;

import java.sql.Date;

import com.cs456.TeamBobcat.database.Constants;
import com.cs456.TeamBobcat.database.MyDB;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ViewSwitcher;
import android.widget.Gallery.LayoutParams;
import android.widget.ImageView.ScaleType;

public class WhatDoesThisSayHistory extends Activity implements
AdapterView.OnItemSelectedListener, ViewSwitcher.ViewFactory {
	
	MyDB dba;
	private Long[] mDate;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);

		dba = new MyDB(this);
		dba.open();
		initImages();
		setContentView(R.layout.whatdoesthissayhistory);

		mSwitcher = (ImageSwitcher) findViewById(R.id.switcher);
		mSwitcher.setFactory(this);
		mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
        android.R.anim.fade_in));
		mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
        android.R.anim.fade_out));

		Gallery g = (Gallery) findViewById(R.id.gallery);
		g.setAdapter(new ImageAdapter(this));
		g.setOnItemSelectedListener(this);
	}

	public void onItemSelected(AdapterView parent, View v, int position, long id) {
		Log.d("PHILMONROE", "ON ITEM SELECTED-------------------------" );
//		mSwitcher.			
		BitmapFactory.Options opts = new BitmapFactory.Options();
		opts.inSampleSize = 64;
//		mSwitcher.setImageBitmap(BitmapFactory.decodeFile(mImageIds[position], opts));
		mSwitcher.setImageDrawable(null);
		System.gc();
		mSwitcher.setImageURI(Uri.parse("file://"+mImageIds[position]));
		TextView tt = (TextView) findViewById(R.id.WDTStext);
		tt.setText(results[position]);
		Date dt = new Date(mDate[position]);
		TextView date = (TextView) findViewById(R.id.date);
		date.setText(android.text.format.DateFormat.format("yyyy-MM-dd hh:mm:ss", dt));
		
	}

	public void onNothingSelected(AdapterView parent) {
	}

	public View makeView() {
		ImageView i = new ImageView(this);
		i.setBackgroundColor(0xFF000000);
		i.setScaleType(ImageView.ScaleType.FIT_CENTER);
		i.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.FILL_PARENT));
		return i;
	}

	private ImageSwitcher mSwitcher;

	public class ImageAdapter extends BaseAdapter {
		public ImageAdapter(Context c) {
			mContext = c;
		}

		public int getCount() {
			return mImageIds.length;
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			ImageView i = new ImageView(mContext);

			//i.setImageURI(Uri.parse("file://"+mImageIds[position]));
			BitmapFactory.Options opts = new BitmapFactory.Options();
			opts.inSampleSize = 32;
			i.setImageBitmap(BitmapFactory.decodeFile(mImageIds[position], opts));
			i.setAdjustViewBounds(true);
			i.setLayoutParams(new Gallery.LayoutParams(
					LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
			//i.setBackgroundResource(R.drawable.picture_frame);
			return i;
		}

		private Context mContext;

	}

	private String[] results;
	private String[] mImageIds;

	private void initImages() {
	
		Cursor prevImages = dba.getWDTSphotos();
		int i=0;
		results= new String[prevImages.getCount()];
		mImageIds= new String[prevImages.getCount()];
    	mDate = new Long[prevImages.getCount()];
		prevImages.moveToFirst();
		while (prevImages.isAfterLast() == false) {
			int index = prevImages.getColumnIndex(Constants.CONTENT_NAME);
			int rindex = prevImages.getColumnIndex(Constants.WDTS_TEXT);
        	int index3 = prevImages.getColumnIndex(Constants.DATE_NAME);
			results[i] = prevImages.getString(rindex);
			mImageIds[i] = prevImages.getString(index);
            mDate[i] = prevImages.getLong(index3);
			i++;
			prevImages.moveToNext();
      		}
	}
}